from PySide6.QtCore import Qt
from PySide6.QtGui import QResizeEvent

from ..base import WordFrameBase, ReturnView
from PySide6.QtWidgets import QVBoxLayout
from common import PptToPdfThread
from qfluentwidgets import StateToolTip, InfoBar


class PptToPdfView(ReturnView):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.frameBase = WordFrameBase()
        self.frameBase.setTitle('PPT 转 PDF')
        self.frameBase.setLabelText('PPT文件所在路径：', 'PDF文件保存路径：')

        self.stateToolTip = StateToolTip('PPT To PDF', 'PPT正在转换PDF，请稍后...', self)
        self.stateToolTip.hide()

        self.to_pdf_thread = PptToPdfThread(self)

        self.frameBase.started.connect(self.start_event)

        self.returnButton.clicked.connect(self.returned_event)
        self.to_pdf_thread.started.connect(self.started_thread)
        self.to_pdf_thread.error.connect(self.error_thread)
        self.to_pdf_thread.finished.connect(self.finished_thread)

        self.vBox.addWidget(self.frameBase)

    def start_event(self):
        ppt_dir, save_dir = self.frameBase.lineEditText()
        if ppt_dir is False or save_dir is False:
            return

        self.to_pdf_thread.set_value(ppt_dir, save_dir)
        self.to_pdf_thread.start()

    def started_thread(self):
        self.stateToolTip.show()

    def error_thread(self, msg):
        InfoBar.error('错误', msg, duration=5000, parent=self)

    def finished_thread(self):
        self.stateToolTip.hide()
        InfoBar.success('成功', '转换完成', duration=3000, parent=self)

    def resizeEvent(self, event: QResizeEvent):
        self.stateToolTip.move(event.size().width() - self.stateToolTip.width() - 20, 10)
        super().resizeEvent(event)

    def returned_event(self):
        self.frameBase.clear()
        self.returned.emit()
